﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ShineTech.Domain.SeedWork;
using ShineTech.Web.Helpers;
using ShineTech.Web.Missons.Commands;
using ShineTech.Web.Missons.Requests;
using ShineTech.Web.Users.Commands;
using ShineTech.Web.Users.Requests;

namespace ShineTech.Web.Users
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : BaseController
    {
        private readonly IMediator _mediator;

        public UserController(IMediator mediator)
        {
            _mediator = mediator;

        }

        [HttpGet("{id}")]
        [ProducesResponseType(typeof(UserDTO), StatusCodes.Status200OK)]
        [ProducesResponseType(StatusCodes.Status404NotFound)]
        public async Task<IActionResult> Get([FromRoute]Guid id)
        {
            UserDTO user = await _mediator.Send(new UserByIdQuery(id));
            if (user == null)
                return NotFound();
            return Ok(user);
        }

        [HttpGet(Name = "UserByFilters")]
        [ProducesResponseType(typeof(IEnumerable<UserDTO>), StatusCodes.Status200OK)]
        public async Task<IActionResult> Get([FromQuery] string name)
        {
            
            IEnumerable<UserDTO> result = await _mediator.Send(new UserByFiltersQuery(name));
            return Ok(result);
        }






        [HttpPost("Register", Name = "Register")]
        [ProducesResponseType(typeof(UserDTO), StatusCodes.Status201Created)]
        [ProducesResponseType(typeof(string), StatusCodes.Status400BadRequest)]
        public async Task<IActionResult> Post([FromBody] CreateUserRequest request)
        {
            try
            {
                if (!ModelState.IsValid)
                    return BadRequest(ModelState);

                var command = new CreateUserCommand(request.Name, request.Email, request.Password,  request.Phone);
                UserDTO user = await _mediator.Send(command);
                return Created(string.Empty, user);
            }
            catch (CustomException ex)
            {
                return BadRequest(ex.Message);
            }

        }

        [HttpPost("Login", Name = "Login")]
        [ProducesResponseType(typeof(UserTokenDTO), StatusCodes.Status201Created)]
        [ProducesResponseType(typeof(string), StatusCodes.Status400BadRequest)]
        public async Task<IActionResult> Post([FromBody] UserLoginRequest request)
        {
            try
            {
                if (!ModelState.IsValid)
                    return BadRequest(ModelState);
                
                
                var command = new UserLoginCommand( request.Email, request.Password);
                UserTokenDTO user = await _mediator.Send(command);
                return Ok(user);
            }
            catch (CustomException ex)
            {
                return BadRequest(ex.Message);
            }

        }
    }
}
